package cn.wyl.org.service.impl;

import cn.wyl.basic.dto.ShopDto;
import cn.wyl.basic.exception.BusinessException;
import cn.wyl.basic.service.impl.BaseServiceImpl;
import cn.wyl.basic.util.BaiduAuditUtils;
import cn.wyl.basic.util.EmailUtil;
import cn.wyl.org.domain.Employee;
import cn.wyl.org.domain.Shop;
import cn.wyl.org.domain.ShopAuditLog;
import cn.wyl.org.mapper.EmployeeMapper;
import cn.wyl.org.mapper.ShopAuditLogMapper;
import cn.wyl.org.mapper.ShopMapper;
import cn.wyl.org.service.IEmployeeService;
import cn.wyl.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {
    @Autowired
    private  EmailUtil emailUtil;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    //店铺审核日志mapper
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 店铺入驻
     * @param shop
     */
    @Override
    @Transactional
    public void settlement(Shop shop) throws Exception {

        //1.非空校验
        if (StringUtils.isEmpty(shop.getName())||
                StringUtils.isEmpty(shop.getTel())||
                StringUtils.isEmpty(shop.getAddress())||

                StringUtils.isEmpty(shop.getAdmin().getUsername())||
                StringUtils.isEmpty(shop.getAdmin().getPhone())||
                StringUtils.isEmpty(shop.getAdmin().getEmail())||
                StringUtils.isEmpty(shop.getAdmin().getPassword())||
                StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())


        ){
            throw new BusinessException("参数不能为空");
        }
        //店铺名合规校验，第三方百度api调用
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名不合规，请重新输入");
        }
        // 2.两次密码不一致校验
         Employee admin= shop.getAdmin();
         if (!admin.getPassword().equals(admin.getComfirmPassword())){
             throw new BusinessException("两次密码不一致，请重新输入");
         }
        //3.店铺是否注册过校验 地址或电话有一个一样则认为重复
        Shop dbShop=shopMapper.getShopByMsg(shop);
         if (dbShop!=null){
             throw new BusinessException("该店铺已被注册过");
         }else {
             //4.把shop和employee保存在表中

             //保存店铺管理员信息t_employee
             //admin保存在employee表中会返回一个自增长主键给admin
             employeeService.save(admin);

             //admin_id保存在shop中，shop再保存在t_shop
             shop.setAdmin_id(admin.getId());
             shopMapper.save(shop);

             //将店铺的id更新到t_employee中
             admin.setShop_id(shop.getId());
             employeeMapper.update(admin);

         }

    }
    //接收前端传入的店铺审核日志，ShopAuditLog log包含参数shopId,note 通过
    @Override
    //有update save性质
    @Transactional
    public void pass(ShopAuditLog log) throws MessagingException {
        //通过店铺审核日志ShopAuditLog log获取审核的店铺
        Shop passShop = shopMapper.loadById(log.getShop_id());
        //修改店铺的状态为2，即审核通过待激活
        passShop.setState(2);
        //店铺更新到数据库
        shopMapper.update(passShop);

        //添加审核日志到数据库
        //设置审核日志状态为2，即审核通过待激活
        log.setState(2);
        //审核日志设置管理员，暂时写死
        log.setAudit_id(325l);
        shopAuditLogMapper.save(log);

        //发送审核通过邮件 - 让客户激活
        //获取店铺管理员的邮箱
        String email = employeeMapper.loadById(passShop.getAdmin_id()).getEmail();
        emailUtil.sendSucess(email,passShop);

    }
    //传入店铺审核日志，ShopAuditLog log包含参数shopId,note 失败（驳回）
    @Override
    @Transactional
    public void reject(ShopAuditLog log) throws MessagingException {
        //通过店铺审核日志ShopAuditLog log获取审核的店铺
        Shop rejectShop = shopMapper.loadById(log.getShop_id());

        /*//修改店铺的状态为4，即审核驳回
        rejectShop.setState(4);
        //店铺更新到数据库
        shopMapper.update(rejectShop);*/

        //添加审核日志到数据库
        //设置审核日志状态为4，即审核驳回
        log.setState(4);
        //审核日志设置管理员，暂时写死
        log.setAudit_id(325l);
        shopAuditLogMapper.save(log);

        //发送审核失败邮件 - 让客户重新注册
        //获取店铺管理员的邮箱
        String email = employeeMapper.loadById(rejectShop.getAdmin_id()).getEmail();
        emailUtil.sendFail(email,rejectShop);

        //删除入驻信息
        //删除管理员信息,联动删除登录表信息
        employeeService.remove(rejectShop.getAdmin_id());
        //删除店铺信息
        shopMapper.remove(rejectShop.getId());


    }

    @Override
    public List<ShopDto> getCountByState() {
        return shopMapper.getCountByState();
    }

    @Override
    @Transactional
    public void save(Shop s){
        //只能从employee中指派管理员
        shopMapper.save(s);

        Long admin_id = s.getAdmin_id();
        if (admin_id!=null){
            Employee admin = employeeMapper.loadById(admin_id);
            //employee只变更了shopid没必要更新登录信息表
            admin.setShop_id(s.getId());
            employeeMapper.update(admin);
        }

    }



    /**
     * 更新店铺，关联admin变动shopid
     * @param s
     */
    @Override
    @Transactional
    public void update(Shop s){
        //只能从employee中指派管理员
        shopMapper.update(s);

        Long admin_id = s.getAdmin_id();
        if (admin_id!=null){
            Employee admin = employeeMapper.loadById(admin_id);
            //employee只变更了shopid没必要更新登录信息表
            admin.setShop_id(s.getId());
            employeeMapper.update(admin);
        }

    }


    @Transactional
    @Override
    public void remove(Long id){
        Long admin_id = shopMapper.loadById(id).getAdmin_id();
        if (admin_id!=null){
            Employee admin = employeeMapper.loadById(admin_id);
            admin.setShop_id(null);
        }
        shopMapper.remove(id);

    }
}
